<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<title>建设阶段</title>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">

<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/vivo.css">

<script src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/layui-v2.1.5/layui/layui.all.js"></script>
<script type="text/javascript" src="js/form.js"></script>
<!-- 为了使用里面数组扩展indexof方法 -->
<script type="text/javascript" src="js/sharefunction.js"></script>
<script language="javascript" type="text/javascript"
	src="<%=basePath%>js/My97DatePicker/WdatePicker.js"></script>

<style type="text/css">
.style_btn {
	margin: 5px;
}
</style>
<script type="text/javascript">
	$(function(){
		mySubmit("buildStage", initBuildStage, successBuild);
		getBuildStage();
		//鼠标悬浮提示
		toolTip(true);
	});
	
	//获取建筑阶段
	function getBuildStage() {
		var url = "project/approval/getBuildStageByIdAndPaId";
		//paid:立项id
		var id = '${id}';
		var paId = parent.$("#fkPa").val();
		$.getJSON(url, {
			id : id,
			paId:paId
		}, function(data, status) {
			if (status == "success") {
				var v,$manage,m;
				if(data != null){
					$("#buildStage input[name='buildDate']").val(new Date(data.buildDate).format('yyyy-MM-dd'));
					$("#buildStage input[name='buildName']").val(data.buildName);
					$("#buildStage input[name='buildUnit']").val(data.buildUnit);
					$("#buildStage input[name='allJobUnit']").val(data.allJobUnit);
					$("#buildStage input[name='allJobScope']").val(data.allJobScope);
					$("#buildStage input[name='projectAssociate']").val(data.projectAssociate);
					$("#buildStage input[name='businessUnit']").val(data.businessUnit);
					$("#buildStage input[name='superviseUnit1']").val(data.superviseUnit);
					$("#buildStage input[name='subpackageId']").val(data.subpackageId);
					$("#buildStage input[name='superviseContact']").val(data.superviseContact);
					$("#buildStage input[name='superviseTel']").val(data.superviseTel);
					$("#buildStage input[name='buId']").val(data.id);
					//添加总包单位列表
					if(data.manageList!=null){
						for(var a=0;a<data.manageList.length;a++){
							addDivManageUnit();
						}
						$manage = $("#div_build").find(".manageUnit");
						for(var i=0;i<data.manageList.length;i++){
							m=$manage[i];
							$(m).find("input[name='id']").val(data.manageList[i].id);
							$(m).find("input[name='allJobUnit']").val(data.manageList[i].allJobUnit);
							$(m).find("input[name='allJobScope']").val(data.manageList[i].allJobScope);
							$(m).find("input[name='contacts']").val(data.manageList[i].contacts);
							$(m).find("input[name='contactNumber']").val(data.manageList[i].contactNumber); 
							 //添加分包div模板并赋值
							 var subList=data.manageList[i].subUnitList;//
							 if(subList!=null && subList.length !=0){
								for(var s=0;s<subList.length;s++){
								$(m).find("a[name='addbtn']").click();
									//addDiv2();
								}
								$t = $(m).parent().find("#div_sub_build table");
								for(var j=0;j<subList.length;j++){
									v=$t[j];
									$(v).find("input[name='superviseUnit']").val(subList[j].superviseUnit);
									$(v).find("input[name='businessUnit']").val(subList[j].businessUnit);
									$(v).find("input[name='unitName']").val(subList[j].unitName);
									//$(v).find("input[name='unitTel']").val(data.subpackage[i].unitTel);
									$(v).find("input[name='principalTel']").val(subList[j].principalTel);
									$(v).find("textarea[name='remark']").val(subList[j].remark);
									$(v).find("input[name='id']").val(subList[j].id);
								}
							}  
							
						}
					}
					
					//隐藏按钮
					$("#buildStage #btn_div2").show();
					$("#buildStage #btn_div1").hide();
					$("#buildStage").find("input,textarea").attr("readonly",true);
					$("#buildStage").find(".addbtn,.Wdate").attr("disabled",true);
					$("#buildStage").find(".icon-fullscreen").hide();
				}
			}
		});
	}
	
	var optBuildStage;
		//optPlanStage操作字段，0：临时保存；1：新增；2：修改
		function saveBuildStage(val){
			optBuildStage=val;
			if(val==3){
				var approvalCount = ${approvalCount};
				if(approvalCount == 0 ){
					//解除锁定状态
					var index=layer.confirm('确定开启编辑状态？', {
					 	offset:'20%',
						shade:0
					}, function(){
					  //去除只读开启按钮编辑
					   $("#buildStage").find("#changeRemark").show();
					   $("#buildStage").find("input,textarea").attr("readonly",false);
					   $("#buildStage").find("select,.addbtn,.Wdate").attr("disabled",false);
					   $("#buildStage").find(".icon-fullscreen").show();
					   $("#buildStage #modifyBtn").hide();
					   $("#buildStage .hidbtn").addClass("showbtn");
					   layer.close(index);
					});
				}else{
					layer.msg("此条数据已提交过审批，不能重复提交");
				}
			}else{
				$("#buildStage").submit();
			}
		}
</script>
</head>
<body>
	<form class="cmxform" id="buildStage" method="post"
		action="project/approval/addBuild" onsubmit="return false">
		<div style="padding:0px 10px">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h5 class="panel-title" style="font-weight: bold;">建设阶段</h5>
				</div>
				<div class="panel-body">
					<input type="hidden" name="buId" id="entityId_buId"/><!-- 实体id -->
					<table cellpadding="10" cellspacing="5" class="mytable">
						<tr>
							<td width="70">建设时间</td>
							<td width="260"><input name="buildDate" readonly="readonly" class="Wdate" onClick="WdatePicker()" /></td>
							<td width="70">建设名称</td>
							<td><input name="buildName" class="toolTip" value="" /></td>
							<td width="70">建设单位</td>
							<td><input name="buildUnit" value="" class="input_name toolTip"/></td>
						</tr>
					</table>
			       	<!-- <h4>施工单位</h4>  -->
			       	<div><span class="style_btn">施工单位</span> <a class="btn btn-danger radius a_font addbtn" onclick="addDivManageUnit()" style="margin-top:5px">添加总包</a></div>
					<!-- 动态div start -->
					<div id="div_build"></div>
					<!-- 动态div end-->
					<p style="margin:10px 0px 0px 0px;">
						<span>监理单位</span> <span style="margin:0px 105px 0 10px;"><input class="toolTip" value="" name="superviseUnit1" /> </span>
						<span>联系人</span> <span style="margin:0px 70px 0 10px;"><input value="" name="superviseContact" /> </span>
						<span>联系电话</span> <span style="margin:0px 0px 0 10px;"><input value="" name="superviseTel" /> </span>
					</p>
				</div>
			</div>
			<!-- 修改时添加变更说明 -->
			<div style="margin:10px;" hidden="hidden" id="changeRemark">
				变更说明<textarea rows="3" name="changeRemark"style="width:70%"></textarea><span style="color:red;">*</span>
			</div>
			<input type="hidden" id="entityId_subpackageId" name="subpackageId" value=""/><!-- 施工单位外键id -->
			<div style="width:100%;text-align:center; padding-top:0px">
			<div id="btn_div1">
				<button class="btn btn-danger radius my-btn"
					onclick="saveBuildStage(0)" type="button">提交</button>
				<button class="btn btn-danger radius my-btn"
					onclick="saveBuildStage(1)" type="button">保 存</button>
				<a class="btn btn-default radius" type="button" onClick="resetForms('buildStage');">
					重置 </a>
				<br />
			</div>
			<div id="btn_div2" style="display:none;">
				<shiro:hasPermission name="permission:edit">
					<button id="modifyBtn" type="button" class="btn btn-danger radius my-btn"
						onclick="saveBuildStage(3)">启用编辑</button>
					<button class="btn btn-danger radius my-btn hidbtn"
						onclick="saveBuildStage(2)" type="button">保存修改</button>
					<button class="btn btn-default radius hidbtn" type="button" onClick="resetForms('buildStage');">
						重置 </button>	
				</shiro:hasPermission>
			</div>	
			</div>
		</div>
	</form>

	<script type="text/javascript">
		function addDiv2(obj) {
			var node;
			node = $("#build_templete>div").clone(true);
			var oTest = $(obj).parent().parent().parent().parent().parent().find("#div_sub_build");
			oTest.append(node);
		}
		//添加总包单位
		function addDivManageUnit() {
			var node;
			node = $("#manage_templete>div").clone(true);
			var oTest = $("#div_build");
			oTest.append(node);
		}
		
		//存储将要删除的分包单位id，传到后台删除
		//var delIds = new Array();
		var delIds = "";
		function delTrDivBuild(obj) {
			var bsid = $(obj).parent().parent().parent().parent().find("input[name='id']").val();
			//delIds.push(bsid);
			delIds += bsid+",";
			$(obj).parent().parent().parent().parent().parent().remove();
			layer.msg("点击保存后，将彻底删除该条数据！");
		}
		//存储将要删除的分包单位id，传到后台删除
		//var delManegeIds = new Array();
		var delManegeIds = "";
		function delTrDivManage(obj) {
			var manageId = $(obj).parent().parent().parent().parent().find("input[name='id']").val();
			//delManegeIds.push(manageId);
			delManegeIds += manageId+",";
			$(obj).parent().parent().parent().parent().parent().remove();
			layer.msg("点击保存后，将彻底删除该条数据！");
		}
		var i;
		//封装表单数据为json
		function initBuildStage() {
			i = layer.load(2);
			var subpackageId = $("#entityId_subpackageId").val();
			//封装基础信息
			var id = $("input[name='buId']").val();
			var buildDate = $("input[name='buildDate']").val();
			var buildName = $("input[name='buildName']").val();
			var buildUnit = $("input[name='buildUnit']").val();
			var superviseUnit = $("input[name='superviseUnit1']").val();
			var superviseContact = $("input[name='superviseContact']").val();
			var superviseTel = $("input[name='superviseTel']").val();
			var fkPa = window.parent.$("#fkPa").val();
			var designId = parent.$("#entityId_designId").val();
			var changeRemark = $("#buildStage textarea[name='changeRemark']").val();
			
			//封装总包单位信息
			var manageUnits = $("#div_build").find(".manageUnit");
			var a;
			var manageList = [];//总包单位列表
			if (manageUnits.length > 0) {
				for (var i = 0; i < manageUnits.length; i++) {
					a = packageUnitMsg(manageUnits[i]);//封装总包单位数据
					if (a == null) {
						return null;
					} else {
						manageList.push(a);
					}
				}
			}
			if(id!=""){
				if(changeRemark==""){
					layer.alert("变更说明不能为空");
					layer.close(i);
					return null;
				}
			}
			if (buildDate == "") {
				layer.alert("建设时间不能为空");
				layer.close(i);
				return null;
			}else if(!valLength(50,buildName)){
				layer.msg("建设名称字数不能超过50字");
				$("input[name='buildName']").focus();
				layer.close(i);
				return null;
			}else if(!valLength(50,buildUnit)){
				layer.msg("建设单位字数不能超过50字");
				$("input[name='buildUnit']").focus();
				layer.close(i);
				return null;
			}
			/* 20180118 暂时去掉该页面所有手机号验证 
			else if(!is_mobile(businessUnit)){
				layer.msg("请输入有效手机号码");
				$("input[name='businessUnit']").focus();
				layer.close(i);
				return null;

			}else if(!is_mobile(superviseTel)){
				layer.msg("请输入有效手机号码");
				$("input[name='superviseTel']").focus();
				layer.close(i);
				return null;

			} */
			else {
				return {
					id:id,
					fkProjectApproval : fkPa,
					buildDate : buildDate,
					buildName : buildName,
					buildUnit : buildUnit,
					superviseUnit : superviseUnit,
					superviseContact:superviseContact,
					superviseTel:superviseTel,
					opt:optBuildStage,
					delIds : delIds,
					designId:designId,
					changeRemark:changeRemark,
					manageList:manageList,
					delManageIds:delManegeIds
				};
	
			}
	
		}
		//封装总包单位信息
		function packageUnitMsg($unit){
			var x=layer.load(2);
			$unit = $($unit);
			var fkBuildstage = $("#entityId_subpackageId").val();
			var manageId=$unit.find("input[name='id']").val();
			var allJobUnit = $unit.find("input[name='allJobUnit']").val();
			var allJobScope = $unit.find("input[name='allJobScope']").val();
			var contacts = $unit.find("input[name='contacts']").val();
			var contactNumber = $unit.find("input[name='contactNumber']").val(); 
			//封装分包单位数据
			var $t = $unit.parent().find("#div_sub_build .subUnit");
			var build = new Array();
			//用来记录分包单位名称，以判断是否重复
			var soleArr=new Array();
			var b=true;//分包单位如果重复该值会为false
			$.each($t, function(i, v) {
				var superviseUnit = $(v).find("input[name='superviseUnit']").val();
				if(soleArr.indexOf(superviseUnit)!=-1){//判断分包单位名称是否重复
					b=false;
					return b;
				}
				soleArr.push(superviseUnit);
				var businessUnit = $(v).find("input[name='businessUnit']").val();
				var unitName = $(v).find("input[name='unitName']").val();
				var principalTel = $(v).find("input[name='principalTel']").val();
				var remark = $(v).find("textarea[name='remark']").val();
				var id = $(v).find("input[name='id']").val();
				/* 20180118 暂时去掉该页面所有手机号验证 
				if (!is_mobile(principalTel)) {
					layer.msg("请输入有效手机号");
					$(v).find("input[name='principalTel']").focus();
					layer.close(i);
					return null;
				}else */
					
				if(remark.length>200){
					layer.msg("备注不能超过200字");
					$(v).find("textarea[name='remark']").focus();
					layer.close(x);
					return null;
				}
				build.push({
					id:id,
					superviseUnit : superviseUnit,
					businessUnit : businessUnit,
					unitName : unitName,
					//unitTel : unitTel,
					principalTel : principalTel,
					remark : remark,
					fkSubpackageId:manageId
				});
			});
			if (allJobUnit == "") {
				layer.alert("总包单位不能为空");
				layer.close(x);
				return null;
			}else if(allJobUnit.length>20){
				layer.alert("总包单位不能超过50字");
				layer.close(x);
				return null;
			}else if(!b){
				layer.alert("分包单位不能重复");
				layer.close(x);
				return null;
			}else{
				layer.close(x);
				return {
					id : manageId,
					allJobUnit : allJobUnit,
					allJobScope : allJobScope,
					contacts : contacts,
					contactNumber : contactNumber,
					fkBuildstage : fkBuildstage,
					subUnitList:build
				};
			}
		}
	
		//成功后调用方法
		function successBuild(responseText) {
			layer.close(i);
			if (responseText.success) {
				layer.alert("保存成功！", {
					offset:'20%',
					shade:[0.1,'#fff']
				}, function() {
					parent.location.reload();
					layer.close(i);
				});
			} else {
				alert(responseText.msg, {icon : 7});
				layer.closeAll();
			}
		}
	</script>
	
	<!-- 总包单位模板div start -->
	<div id="manage_templete" style="display:none;">
		<div class="style_border_tem">
			<table cellpadding="10" cellspacing="5" class="mytable manageUnit">
				<tr>
					<td width="90"><span class="style_btn">总包单位</span><input type="hidden" id="entityId_id" name="id" value="" /></td>
					<td width="320"><input name="allJobUnit" class="toolTip" value="" /><span style="color:red"> *</span></td>
					<td width="90"><span class="style_btn">总包范围</span></td>
					<td><input name="allJobScope" value="" class="toolTip" /></td>
					<td  valign="top" align="right"><a
						href="javascript:void(0)" onclick="delTrDivManage(this)"> <i
							class="icon-fullscreen"></i>
					</a></td>
				</tr>
				<tr>
					<td><span class="style_btn">联系人</span></td>
					<td><input name="contacts" value="" /></td>
					<td><span class="style_btn">联系人电话</span></td>
					<td colspan="2"><input name="contactNumber" style="margin-right:15px;" value="" />
						<a class="btn btn-danger radius a_font addbtn" name="addbtn" onclick="addDiv2(this)">
							添加分包</a></td>
				</tr>
			</table>
			<!-- 动态div start -->
			<div id="div_sub_build"></div>
			<!-- 动态div end-->
		</div>
	</div>
	<!-- 模板div start -->
	<div id="build_templete" style="display:none;">
		<div class="style_border_tem">
			<table cellpadding="10" cellspacing="5" class="mytable subUnit">
				<tr>
					<td width="90"><span class="style_btn">分包单位</span><input type="hidden" id="entityId_id" name="id" value="" /></td>
					<td><input id="" class="toolTip" name="superviseUnit" value="" /></td>
					<td width="90"><span class="style_btn">分包范围</span></td>
					<td><input id="" name="businessUnit" value="" /></td>
					<td  valign="top" align="right"><a
						href="javascript:void(0)" onclick="delTrDivBuild(this)"> <i
							class="icon-fullscreen"></i>
					</a></td>
				</tr>
				<!-- <tr>
					<td width="90"><span>业务单位</span></td>
					<td width="320"><input id="" name="businessUnit" value="" /></td>
					<td width="90"><span>单位名称</span></td>
					<td><input id="" name="unitTel" value="" /></td>
				</tr> -->
				<tr>
					<td width="90"><span class="style_btn">联系人</span></td>
					<td width="320"><input id="" name="unitName" value="" /></td>
					<td width="90"><span class="style_btn">联系人电话</span></td>
					<td><input id="" name="principalTel" value="" /></td>
				</tr>
				<tr>
					<td><span class="style_btn">备注</span></td>
					<td colspan="3"><textarea name="remark" rows="3" cols="70" style="width:70%;" maxlength="200" onkeydown="checkMaxInput(this,200)"  
            						onkeyup="checkMaxInput(this,200)" onfocus="checkMaxInput(this,200)" onblur="checkMaxInput(this,200);resetMaxmsg()"></textarea></td>
				</tr>
			</table>
		</div>
	</div>
	<!-- 模板div end -->
</body>
</html>

